package com.slipper.sfts.member.front;

import com.slipper.common.constants.Constants;
import com.slipper.common.util.StringUtil;
import com.slipper.sfts.member.service.MemberBrokerAccountService;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

import com.slipper.common.bo.Result;
import com.slipper.common.util.ResultUtil;
import com.slipper.sfts.api.member.bo.MemberBO;
import com.slipper.sfts.api.member.service.IMemberSv;
import com.slipper.sfts.member.service.MemberService;

import java.util.List;

@RestController
public class MemberSv implements IMemberSv {
    @Resource
    private MemberService memberService;
    @Resource
    private MemberBrokerAccountService memberBrokerAccountService;
    
    @Override
    public Result<MemberBO> queryBy(String id) {
        MemberBO member = memberService.queryBy(id);
        member.setAccountList(memberBrokerAccountService.queryListByMemberId(id));
        return ResultUtil.successResult(member);
    }

    @Override
    public Result<List<MemberBO>> queryList(MemberBO bo) {
        return ResultUtil.successResult(memberService.queryList(bo));
    }
    
    @Override
    public Result<List<MemberBO>> queryPage(MemberBO bo) {
        return ResultUtil.successResult(memberService.queryPage(bo));
    }
    
    @Override
    public Result<?> save(MemberBO bo) {
        return ResultUtil.saveResult(memberService.save(bo), bo);
    }
    
    @Override
    public Result<?> delete(MemberBO bo) {
        return ResultUtil.deleteResult(memberService.delete(bo));
    }

    @Override
    public Result<MemberBO> queryByLogin(MemberBO bo) {
        if (StringUtil.isBlank(bo.getUsername())) {
            return ResultUtil.failureResult("用户名不能为空！");
        }
        return ResultUtil.successResult(memberService.queryByLogin(bo));
    }

    @Override
    public Result<?> register(MemberBO bo) {
        if (StringUtil.isBlank(bo.getUsername())) {
            return ResultUtil.failureResult("用户名不能为空！");
        }
        if (StringUtil.isBlank(bo.getPassword())) {
            return ResultUtil.failureResult("密码不能为空！");
        }
        if (memberService.queryByLogin(bo) != null) {
            return ResultUtil.failureResult("此用户名已被占用，请重新输入！");
        }

        bo.setValid(Constants.YES);
        bo.setApproveStatus(MemberBO.APPROVE_STATUS_PENDING);
        return ResultUtil.saveResult(memberService.save(bo));
    }

    @Override
    public Result<?> queryBrokerAccountList(String id) {
        return ResultUtil.successResult(memberBrokerAccountService.queryListByMemberId(id));
    }
}
